package org.rzp.ideaplug.statistic.statistic;

import javax.swing.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class StatisticUtil {

    public static StatisticInfo statistic(String path) {
        return statistic(path, null, null, null);
    }


    static StatisticInfo statistic(String path, JProgressBar jProgressBar, JLabel jProcessText, JTextArea jLabelStatistic) {
        File f = new File(path);
        if (f.isDirectory()) {
            List<File> javaFiles = searchFileListByType(null, f, "java");
            return calcJavaFileStatistic(javaFiles, jProgressBar, jProcessText, jLabelStatistic);
        } else {
            return calcJavaFileStatistic(Arrays.asList(f), jProgressBar, jProcessText, jLabelStatistic);
        }
    }


    private static List<File> searchFileListByType(List<File> result, File f, String type) {
        if (null == result) {
            result = new ArrayList<>();
        }
        File[] listFiles = f.listFiles();

        for (File ff : listFiles) {
            if (ff.isDirectory()) {
                if (!ff.getName().startsWith("."))
                    searchFileListByType(result, ff, type);
            } else {
                if (ff.getName().endsWith("." + type)) {
                    result.add(ff);
                }
            }
        }
        return result;
    }


    private static StatisticInfo calcJavaFileStatistic(List<File> fileList, JProgressBar jProgressBar, JLabel jProcessText, JTextArea jLabelStatistic) {
        int k = 0, fileTotal = fileList.size();
        int totalS = 0, codeS = 0, commentS = 0, blankS = 0, annS = 0;
        StatisticInfo statisticInfo = new StatisticInfo();
        String formatS = "";
        for (File f : fileList) {
            int total = 0, code = 0, comment = 0, blank = 0, ann = 0;
            BufferedReader bufferedReader = null;
            try {
                bufferedReader = new BufferedReader(new FileReader(f));
                String line = "";
                boolean superCommentMark = false;
                while (null != (line = bufferedReader.readLine())) {
                    String lineTrim = line.trim();
                    total++;
                    if (superCommentMark) {
                        comment++;
                        if (superCommentMark && lineTrim.endsWith("*/")) {
                            superCommentMark = false;
                        }
                    } else {
                        if (!"".equals(lineTrim)) {
                            if (!superCommentMark && lineTrim.startsWith("/*")) {
                                comment++;
                                superCommentMark = true;
                            } else if (!superCommentMark && lineTrim.startsWith("//")) {
                                comment++;
                            } else if (!superCommentMark && lineTrim.startsWith("@")) {
                                code++;
                                ann++;
                            } else if (!superCommentMark) {
                                code++;
                            }
                        } else {
                            blank++;
                        }
                    }
                }
                /*log.info(f.getName());
                String format = String.format("total: %s, code: %s, comment: %s, blank: %s ,ann: %s", total, code, comment, blank, ann);
                log.info(format);
                log.info("==========");*/

                totalS += total;
                codeS += code;
                commentS += comment;
                blankS += blank;
                annS += ann;

                if (null != jLabelStatistic) {
                    formatS = String.format("total: %s, code: %s, comment: %s, blank: %s ,ann: %s", totalS, codeS, commentS, blankS, annS);
                    //log.info(formatS);
                    jLabelStatistic.setText(formatS);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                statisticInfo.setTotalLine(totalS);
                statisticInfo.setSourceCodeLine(codeS);
                statisticInfo.setCommentLine(commentS);
                statisticInfo.setBlankLine(blankS);
                statisticInfo.setAnnotationLine(annS);

                if (null != jLabelStatistic) {
                    String format = String.format(" 总计 : %s\n 代码 : %s(%s)\n 注释 : %s(%s)\n 空白 : %s(%s)\n 注解 : %s(%s)",
                            totalS, codeS, String.format("%.2f", ((double) codeS / (double) totalS * 100)) + "%",
                            commentS, String.format("%.2f", ((double) commentS / (double) totalS * 100)) + "%",
                            blankS, String.format("%.2f", ((double) blankS / (double) totalS * 100)) + "%",
                            annS, String.format("%.2f", ((double) annS / (double) totalS * 100)) + "%");

                    jLabelStatistic.setText(format);
                }
                k++;
                double per = ((double) k / (double) fileTotal) * 100;
                if (null != jProcessText) {
                    jProcessText.setText((int) per + "%(" + k + "/" + fileTotal + ")");
                }
                if (null != jProgressBar) {
                    jProgressBar.setValue((int) per);
                }
                if (null != bufferedReader) {
                    try {
                        bufferedReader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        return statisticInfo;

    }

}
